package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.db.DBManager;
import com.example.myapplication.model.Teacher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherListActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<Teacher> teacherList;
    private String courseName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_list);

        // 设置ActionBar
        if (getSupportActionBar() != null) {
            String courseName = getIntent().getStringExtra("courseName");
            getSupportActionBar().setTitle("任课教师");
            getSupportActionBar().setSubtitle(courseName);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setElevation(4);
        }

        // 显示加载进度
        View loadingView = findViewById(R.id.loadingProgress);
        if (loadingView != null) {
            loadingView.setVisibility(View.VISIBLE);
        }

        // 获取教师列表
        new Thread(() -> {
            DBManager dbManager = new DBManager(this);
            ArrayList<Teacher> teachers = dbManager.getTeachersByCourseName(courseName);

            // 在主线程更新UI
            runOnUiThread(() -> {
                if (loadingView != null) {
                    loadingView.setVisibility(View.GONE);
                }

                // 准备列表数据
                List<Map<String, String>> data = new ArrayList<>();
                for (Teacher teacher : teachers) {
                    Map<String, String> item = new HashMap<>();
                    item.put("name", teacher.getName());
                    item.put("info", teacher.getInfo());
                    data.add(item);
                }

                // 设置适配器
                SimpleAdapter adapter = new SimpleAdapter(this,
                        data,
                        android.R.layout.simple_list_item_2,
                        new String[] { "name", "info" },
                        new int[] { android.R.id.text1, android.R.id.text2 });

                listView = findViewById(R.id.teacherList);
                if (listView != null) {
                    listView.setAdapter(adapter);
                }

                // 如果列表为空，显示提示
                if (data.isEmpty()) {
                    Toast.makeText(this, "没有找到相关教师", Toast.LENGTH_SHORT).show();
                    finish();
                }
            });
        }).start();
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
